package com.qf.user_service;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;

import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;

import com.baomidou.mybatisplus.generator.config.rules.DateType;

import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;

import java.util.ArrayList;

import java.util.List;


/**
 * @author 刘亚康
 * @Date 2020/3/26
 * @Version 1.0'
 * @Description 代码自动生成器
 */
public class AutoCode {
    public static void main(String[] args) {
        //代码自动生成器
        AutoGenerator autoGenerator=new AutoGenerator();
        //配置策略

        //1：全局配置
        GlobalConfig gc=new GlobalConfig();

        String projectPath=System.getProperty("user.dir"); //设置路径
        gc.setOutputDir(projectPath+"/src/main/java");  //设置输出路径
        gc.setAuthor("刘亚康"); //设置作者
        gc.setOpen(false); //是否打开目录
        gc.setFileOverride(true);//是否覆盖
        gc.setServiceName("I%sService"); //Service名称
        gc.setServiceImplName("%sServiceImpl");
        gc.setMapperName("I%sMapper");
        gc.setIdType(IdType.AUTO);//Id策略
        gc.setDateType(DateType.ONLY_DATE);//时间策略
        gc.setSwagger2(true);//是否生成swagger2文档
        gc.setXmlName("I%sMapper");
        //使能该全局配置
        autoGenerator.setGlobalConfig(gc);

        //设置数据源
        DataSourceConfig dsc=new DataSourceConfig();
        dsc.setDriverName("com.alibaba.druid.proxy.DruidDriver");
        dsc.setUrl("jdbc:mysql://180.76.97.131:3306/shop?useUnicode=true&characterEncoding=UTF-8");
        dsc.setUsername("java");
        dsc.setPassword("java");
        dsc.setDbType(DbType.MYSQL);
        //使能数据源配置
        autoGenerator.setDataSource(dsc);

        //包的配置
        PackageConfig pc=new PackageConfig();
//        pc.setModuleName("user");
        pc.setParent("com.qf");
        pc.setEntity("entity");
        pc.setController("controller");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        //使能该包配置
        autoGenerator.setPackageInfo(pc);



        //策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setTablePrefix("shop_");//数据库表前缀

        strategy.setNaming(NamingStrategy.underline_to_camel);//设置类名配置规则为下划线转驼峰
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//配置字段名策略
        strategy.setEntityLombokModel(true);//自动配置lombok

        List<String> logicDelete=new ArrayList<>();
        logicDelete.add("order_is_delete");
        logicDelete.add("u_delete");
        logicDelete.add("address_is_delete");
        for (String s : logicDelete) {
            strategy.setLogicDeleteFieldName(s);//配置伪删除
        }



        //自动填充配置
        TableFill crete = new TableFill("create_time", FieldFill.INSERT);
        TableFill orderIsDelete = new TableFill("order_id_delete", FieldFill.INSERT);
        TableFill userIsDelete = new TableFill("u_delete", FieldFill.INSERT);
        TableFill addressIsDelete = new TableFill("address_is_delete", FieldFill.INSERT);
        TableFill goodsState = new TableFill("goods_state", FieldFill.INSERT);
        TableFill update = new TableFill("update_time", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills=new ArrayList<>();
        tableFills.add(crete);
        tableFills.add(update);
        strategy.setTableFillList(tableFills);
        //使能该策略
        autoGenerator.setStrategy(strategy);


        //执行
        autoGenerator.execute();
    }


}


